﻿using AutoMapper;
using GGGK.Film.IService;
using GGGK.Film.Models;
using GGGK.Film.Models.Dto;
using GGGK.Film.Models.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace GGGK.Film.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DictInfoController : ControllerBase
    {
        private readonly IDictInfoService svc;
        private readonly IDictVWService vwSvc;
        private readonly IMapper mapper;

        public DictInfoController(IDictInfoService svc,IDictVWService vwSvc, IMapper mapper)
        {
            this.svc = svc;
            this.vwSvc = vwSvc;
            this.mapper = mapper;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="dictName"></param>
        /// <param name="dictTypeId"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        public PagedResult<DictInfoDto> Query(string? dictName, int? dictTypeId = 0, int pageSize = 10, int pageIndex = 1)
        {
            string sql = @"select i.*,t.DictTypeName from Base_DictInfo i
                            inner join Base_DictType t on i.DictTypeId = t.Id and t.[Status] = 1
                            where 1=1";

            if (!string.IsNullOrEmpty(dictName))
                sql += " and i.DictName like @dictName";

            if (dictTypeId != 0)
                sql += " and i.dictTypeId = @dictTypeId";

            string totalSql = "select count(*) from (" + sql + ") ff";

            int total = svc.ExecuteScalar<int>(totalSql, new { dictName = $"%{dictName}%", dictTypeId });

            sql += @" order by i.CreateTime desc
                     offset @pageSize*(@pageIndex-1) rows fetch next @pageSize rows only";

            var data = svc.Query<DictInfoDto>(sql, new { dictName = $"%{dictName}%", dictTypeId, pageIndex, pageSize });

            return new PagedResult<DictInfoDto>() { code = 1, msg = "成功", data = data, total = total };
        }

        /// <summary>
        /// 根据类型名获取
        /// </summary>
        /// <param name="typeName"></param>
        /// <returns></returns>
        [HttpGet("GetByTypeName")]
        public Result<IEnumerable<VW_Dict>> GetByTypeName(string typeName)
        {
            var data = vwSvc.Query(o=>o.DictTypeName==typeName && o.Status ==1);

            return new Result<IEnumerable<VW_Dict>>() { code = 1,msg="成功",data = data};
            
        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public Result Get(int id)
        {
            var entity = svc.Get(o => o.Id == id);

            if (entity == null) return new Result() { code = -1, msg = "数据不存在" };
            else return new Result() { code = 1, msg = "成功", data = entity };
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public Result Post([FromBody] DictInfoPostDto dto)
        {
            var entity = mapper.Map<Base_DictInfo>(dto);

            entity.Status = 1;
            entity.CreateTime = DateTime.Now;

            if (svc.Add(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = 1, msg = "失败" };
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public Result Put(int id, [FromBody] DictInfoPostDto dto)
        {
            var entity = svc.Get(o => o.Id == id);

            if (entity == null) return new Result() { code = -1, msg = "数据不存在" };

            mapper.Map(dto, entity);


            if (svc.Update(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public Result Delete(int id)
        {
            var entity = svc.Get(o => o.Id == id);

            if (entity == null) return new Result() { code = -1, msg = "数据不存在" };

            entity.Status = 0;

            if (svc.Update(entity))
                return new Result() { code = 1, msg = "成功" };
            else
                return new Result() { code = -1, msg = "失败" };
        }
    }
}
